package com.youlin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.youlin.dto.BaseDataResDto;
import com.youlin.dto.Status;
import com.youlin.dto.req.FindAllRelationReqDto;
import com.youlin.dto.res.RelationResDto;
import com.youlin.entities.BygRelation;
import com.youlin.mapper.BygRelationMapper;
import com.youlin.service.BygRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlin.untils.BaseService;
import org.springframework.stereotype.Service;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BygRelationServiceImpl extends ServiceImpl<BygRelationMapper, BygRelation> implements BygRelationService {

    @Override
    public BaseDataResDto<List<RelationResDto>> findAllRelations (FindAllRelationReqDto data) {
        QueryWrapper<BygRelation> queryWrapper = new QueryWrapper<>();
        if (data.getRespectGender() != null) queryWrapper.eq("respect_gender", data.getRespectGender());
        if (data.getTakerGender() != null) queryWrapper.eq("taker_gender", data.getTakerGender());
        if (data.getGrade() != null) queryWrapper.eq("grade", data.getGrade());
        List<RelationResDto> relations = list(queryWrapper).parallelStream()
                .map(r -> BaseService.copyProperties(r, RelationResDto.class))
                .sorted(Comparator.comparingInt(RelationResDto::getId)).collect(Collectors.toList());
        return new BaseDataResDto<List<RelationResDto>>(Status.SUCCESS).setData(relations);
    }

}
